package com.mycompany.biblio.business;

import com.mycompany.biblio.model.FriendRequest;
import com.mycompany.biblio.model.Status;
import com.mycompany.biblio.model.User;
import java.util.List;
import javax.ejb.EJB;
import javax.persistence.*;
import javax.ejb.Stateless;


@Stateless
public class FriendDAO {

    // ======================================
    // =             Attributes             =
    // ======================================
    
    @EJB
    SecurityService securityService;
    
    @PersistenceContext(unitName = "jsfExamplePU")
    private EntityManager em;

    // ======================================
    // =           Public Methods           =
    // ======================================

    public List<User> getAllFriends()
    {
        User user = securityService.getCurrentUser();
        return user.getFriends();
    }

    public FriendRequest create(FriendRequest friend) {
        em.persist(friend);
        return friend;
    }

    public FriendRequest update(FriendRequest friend) {
      
        return em.merge(friend);
    }
    
       public boolean isFriend(User user1, User user2)
    {
        boolean found=false;
        List<User> liFriend = user1.getFriends();
        for (User u : liFriend)
        {
            if (u.getUsername().equals(user2.getUsername()))
            {
                found=true;
                break;
            }
        }
        return found;
    }


}